import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class GetMacPage extends StatefulWidget {
  const GetMacPage({super.key});

  @override
  State<GetMacPage> createState() => _GetMacPageState();
}

class _GetMacPageState extends State<GetMacPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('获取mac地址'),
        actions: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: IconButton(
              onPressed: () {
                setState(() {});
              },
              icon: const Icon(Icons.refresh),
            ),
          )
        ],
      ),
      body: FutureBuilder(
        future: httpRequestMac(),
        builder: (context, snapshot) {
          // 发生错误
          if (snapshot.hasError) {
            return Center(child: Text("${snapshot.error}"));
          }
          // 请求完成
          if (snapshot.hasData) {
            return Center(child: Text("获取mac结果 :  ${snapshot.data!}"));
          }
          // 请求过程中
          return const Center(child: CircularProgressIndicator());
        },
      ),
    );
  }

  Future<String> httpRequestMac() async {
    final response = await http.get(Uri.parse('http://localhost:3000/mac-address'));
    String result = '没有获取到';
    if(response.statusCode == 200){
      result = response.body;
    }
    debugPrint('请求结果： $result');
    return result;
  }
}
